package br.com.dican.newportal.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import br.com.dican.newportal.beans.Representante;
import br.com.dican.newportal.factory.ConectionFactory;

public class RepresentanteDAO {

	public static Representante getRepresentante(String login, String senha) throws Exception{
		
		Representante representante = null;
		Connection conn = ConectionFactory.getConnection();
		
		String query = "SELECT us.ad_client_id, us.name, us.username, us.ad_user_id, us.c_bpartner_id, us.ad_org_id, us.email, org.name as orgName, org.description as orgEmail ";
			   query+= "FROM ad_user us INNER JOIN ad_org org ON us.ad_org_id=org.ad_org_id  ";
		       query+= "WHERE us.username = ? AND us.description = ? LIMIT 1";
		
		PreparedStatement stmt = conn.prepareStatement(query);
		stmt.setString(1, login);
		stmt.setString(2, senha.toLowerCase());
		
		ResultSet rs = stmt.executeQuery();

		if(rs.next()){
			representante = new Representante();
			representante.setBpid(rs.getString("c_bpartner_id"));
			representante.setClientId(rs.getString("ad_client_id"));
			representante.setCodigo(rs.getString("username"));
			representante.setEmail(rs.getString("email"));
			representante.setId(rs.getString("ad_user_id"));
			representante.setNome(rs.getString("name"));
			representante.setOrgEmail(rs.getString("orgEmail"));
			representante.setOrgid(rs.getString("ad_org_id"));
			representante.setOrgNome(rs.getString("orgName"));
			
		}

		stmt.close();
		rs.close();
		conn.close();
		return representante;
	}
}
